package com.sujinyang.stateless_service.config;

import com.sujinyang.stateless_service.shiro.StatelessDefaultSubjectFactory;
import com.sujinyang.stateless_service.shiro.StatelessFilter;
import com.sujinyang.stateless_service.shiro.StatelessRealm;
import com.sujinyang.stateless_service.shiro.StatelessSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.mgt.SubjectDAO;
import org.apache.shiro.mgt.SubjectFactory;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 配置shiro相关信息
 */
@Configuration
public class shiroConfig {

    @Bean("subjectFactory")
    public SubjectFactory subjectFactory() {
        return new StatelessDefaultSubjectFactory();
    }


    @Bean("sessionManager")
    public SessionManager sessionManager() {
        DefaultSessionManager sessionManager = new DefaultSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(false);
        return sessionManager;
    }

    @Bean("statelessRealm")
    public AuthorizingRealm statlessRealm() {
        return new StatelessRealm();
    }

    @Bean("securityManager")
    public SecurityManager securityManager(SessionManager sessionManager,SubjectFactory subjectFactory,AuthorizingRealm statelessRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setSessionManager(sessionManager);
        SubjectDAO subjectDAO = new DefaultSubjectDAO();
        ((DefaultSubjectDAO) subjectDAO).setSessionStorageEvaluator(new StatelessSessionStorageEvaluator());
        defaultWebSecurityManager.setSubjectDAO(subjectDAO);
        defaultWebSecurityManager.setSubjectFactory(subjectFactory);
        defaultWebSecurityManager.setRealm(statelessRealm);
        return defaultWebSecurityManager;
    }


    /**
     * 配置过滤器以及匹配规则
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
        shiroFilterFactory.setSecurityManager(securityManager);
        Map<String,Filter> filterMap = shiroFilterFactory.getFilters();
        filterMap.put("statelessFilter",new StatelessFilter());
        Map<String, String> map = new LinkedHashMap<>();
        map.put("/**","statelessFilter");
        shiroFilterFactory.setFilterChainDefinitionMap(map);
        return shiroFilterFactory;
    }


    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }


    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
